package org.groupg.practice;

import net.sf.jsqlparser.parser.CCJSqlParserUtil;
import net.sf.jsqlparser.statement.Statement;
import net.sf.jsqlparser.statement.comment.Comment;
import net.sf.jsqlparser.statement.create.table.CreateTable;

public class DDLParserExample {
    public static void main(String[] args) throws Exception {
        String ddl = """
                create table  IF NOT EXISTS RWA_REPORT_RS_ORGCREDICTGROUP(
                CALCINDEX                      VARCHAR(60)        Comment '计算实例号',
                SDATE                          VARCHAR(8)         comment '起始数据日期',
                EDATE                          VARCHAR(8)         comment '截止数据日期',
                TERM                           VARCHAR(8)         comment '年月',
                DATA_DATE                      VARCHAR(8)         comment '数据日期',
                ORGCODE                        VARCHAR(60)        comment '经营机构',
                SUM_BALANCE                    VARCHAR(60)        comment '期末资产余额合计',
                ON_BALANCE                     VARCHAR(60)        comment  '资产余额（表内）',
                OFF_BALANCE                    VARCHAR(60)        comment '资产余额（表外）',
                MONTHAVERAGE_BALANCE           DECIMAL(30,6)      comment '月均资产余额',
                ENDOFTERM_BALANCE              DECIMAL(30,6)      comment '期末本期余额合计',
                THISTERM_ON_BALANCE            DECIMAL(30,6)      comment '本期余额（表内）',
                THISTERM_OFF_BALANCE           DECIMAL(30,6)      comment '本期余额（表外）',
                MONTHAVERAGE_THISTERM_BALANCE  DECIMAL(30,6)      comment '月均本期余额',
                ENDOFTERM_ECL                  DECIMAL(30,6)      comment '期末减值准备',
                ENDOFTERM_ON_ECL               DECIMAL(30,6)      comment '期末减值准备（表内）',
                ENDOFTERM_OFF_ECL              DECIMAL(30,6)      comment '期末减值准备（表外）',
                ENDOFTERM_SUM_EAD              DECIMAL(30,6)      comment '期末风险暴露合计',
                ENDOFTERM_ON_EAD               DECIMAL(30,6)      comment '期末风险暴露（表内）',
                ENDOFTERM_OFF_EAD              DECIMAL(30,6)      comment '期末风险暴露（表外）',
                MONTHAVERAGE_EAD               DECIMAL(30,6)      comment '月均风险暴露',
                SUM_RWA                        DECIMAL(30,6)      comment '期末风险加权资产合计',
                ON_RWA                         DECIMAL(30,6)      comment '期末风险加权资产（表内）',
                OFF_RWA                        DECIMAL(30,6)      comment '期末风险加权资产（表外）',
                MONTHAVERAGE_RWA               DECIMAL(30,6)      comment '月均风险加权资产',
                AVERAGE_RISK_WEIGHT            DECIMAL(30,6)      comment '缓释后平均风险权重（%）'
                )COMMENT='各经营机构信用风险资本占用综合监控表';
                
                """;

        // 解析语句
        Statement statement = CCJSqlParserUtil.parse(ddl);
        if (statement instanceof CreateTable createTable) {

            // 获取表名
            String tableName = createTable.getTable().getName();
            System.out.println("Table: " + tableName);
            System.out.println(createTable.getTableOptionsStrings());

            // 遍历列定义
            createTable.getColumnDefinitions().forEach(column -> {
                System.out.println("Column: " + column.getColumnName());
                System.out.println("Type: " + column.getColDataType().getDataType());
                System.out.println("Type: " + column.getColDataType().getArrayData());
                System.out.println("Type: " + column.getColDataType().getCharacterSet());
                System.out.println("Type: " + column.getColDataType().getArgumentsStringList());
                System.out.println(column.getColumnSpecs().toString());
            });
        }
        if (statement instanceof Comment comment) {
            // 获取表名
            String tableName = comment.getTable().getName();
            System.out.println("Table: " + tableName);

            // 遍历列定义
            comment.getColumn().getColumnName();
            comment.getTable().getName();
            comment.getComment().toString();
        }
    }
}